package com.leyou.page.service;

import com.leyou.item.pojo.*;
import com.leyou.page.clients.GoodsClient;
import com.leyou.page.clients.SpecClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageService {
    @Autowired
    private GoodsClient goodsClient;
    @Autowired
    private SpecClient specClient;

    public Map<String, Object> loadData(Long spuId) {
        //根据SpuId查询Spu
        Spu spu = goodsClient.querySpuById(spuId);

        //根据spuId获取spudetail
        SpuDetail spuDetail = goodsClient.querySpuDetailById(spuId);
        //根据spuId 获取skus
        List<Sku> skus = goodsClient.querySkuBySpuId(spuId);
        //获取specGroup
        List<SpecGroup> specGroups = specClient.querySpecGroups(spu.getCid3());
        //获取每个规格组内的规格参数
        specGroups.forEach(specGroup -> {
            specGroup.setSpecParams(specClient.querySpecParam(specGroup.getId(), spu.getCid3(), null, null));
        });
        // 查询商品分类下的特有规格参数
        List<SpecParam> params =
                this.specClient.querySpecParam(null, spu.getCid3(), null, false);
        // 处理成id:name格式的键值对
        Map<Long, String> specMap = new HashMap<>();
        for (SpecParam param : params) {
            specMap.put(param.getId(), param.getName());//4 机身颜色，5，内存
        }
        Map<String, Object> modelMap = new HashMap<>();
        modelMap.put("spu", spu);
        modelMap.put("spuDetail", spuDetail);
        modelMap.put("skus", skus);
        modelMap.put("specGroups", specGroups);
        modelMap.put("specMap", specMap);
        return modelMap;
    }
}
